package com.ffapp.demo.bean;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.ffapp.demo.R;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.greendao.GreenDaoUtils;

import java.util.ArrayList;
import java.util.List;

import my.LogUtil;

public class WarnMapInfoData extends BasisBean {

    List<WarnMapData> baseWarnMapDataList;
    List<Marker> baseMarkerList;
    List<MarkerOptions> baseMarkerOptionsList;

    public  WarnMapInfoData(){
        baseWarnMapDataList = new ArrayList<>();
        baseMarkerList = new ArrayList<>();
        baseMarkerOptionsList = new ArrayList<>();
    }


    private String mapType;
    private AMap aMap;

    public  WarnMapInfoData(String mapType){
        this.mapType = mapType;
        baseWarnMapDataList = new ArrayList<>();
        baseMarkerList = new ArrayList<>();
        baseMarkerOptionsList = new ArrayList<>();
    }


    public  WarnMapInfoData(String mapType,AMap aMap){
        if(baseMarkerList != null && baseMarkerList.size()>0){
            for(Marker marker : baseMarkerList){
                marker.remove();
            }
        }

        this.mapType = mapType;
        this.aMap = aMap;
        baseWarnMapDataList = new ArrayList<>();
        baseMarkerList = new ArrayList<>();
        baseMarkerOptionsList = new ArrayList<>();
    }

    public  WarnMapInfoData(String mapType,AMap aMap,boolean isClicked){
        if(baseMarkerList != null && baseMarkerList.size()>0){
            for(Marker marker : baseMarkerList){
                marker.remove();
            }
        }

        this.mapType = mapType;
        this.aMap = aMap;
        this.isClicked = isClicked;
        baseWarnMapDataList = new ArrayList<>();
        baseMarkerList = new ArrayList<>();
        baseMarkerOptionsList = new ArrayList<>();

    }


    boolean listHaveBean = false;
    boolean isLoadingData = false;
    boolean aMapHaveMarkers = false;
    boolean markersIsVisible = false;
    boolean isClicked = false;


    public void httpIsLoading(){
        listHaveBean = false;
        isLoadingData = true;
    }

    public void httpReturnSuccess(){
        listHaveBean = true;
        isLoadingData = false;
    }

    public void httpReturnFailure(){
        listHaveBean = false;
        isLoadingData = false;
    }

    public void initBaseMarkerList(AMap aMap,String name){
            if(baseMarkerOptionsList.size()>0){
                ArrayList<MarkerOptions>optionList  = new ArrayList<>();
                for (MarkerOptions option : baseMarkerOptionsList){
                    if(option.getTitle().equals(name)){
                        optionList.add(option);
                    }
                }

                ArrayList<WarnMapData> warnMapDataList  = new ArrayList<>();
                for (WarnMapData warnMapData : baseWarnMapDataList){
                    if(warnMapData.getMap_type().equals(name)){
                        warnMapDataList.add(warnMapData);
                    }
                }
                if(optionList.size()>0 && warnMapDataList.size()>0){
                    List<Marker> markerList = new ArrayList<>();
                    markerList.addAll(aMap.addMarkers(optionList, false));
                    for (int i = 0; i < markerList.size(); i++) {
                        Marker marker = markerList.get(i);
                        marker.setObject(warnMapDataList.get(i));
                    }
                    baseMarkerList.addAll(markerList);
                }
            }
    }

    public void initBaseMarkerList(AMap aMap){
        if(!aMapHaveMarkers){
            if(baseMarkerOptionsList.size()>0 && baseWarnMapDataList.size()>0){
                List<Marker> markerList = new ArrayList<>();
                markerList.addAll(aMap.addMarkers((ArrayList<MarkerOptions>) baseMarkerOptionsList, false));
                for (int i = 0; i < markerList.size(); i++) {
                    Marker marker = markerList.get(i);
                    marker.setObject(baseWarnMapDataList.get(i));
                }
                baseMarkerList.addAll(markerList);
            }
            aMapHaveMarkers = true;
        }
    }


    /**
     *           VisibleRegion region = aMap.getProjection().getVisibleRegion();
     *           LatLngBounds latLngBounds = region.latLngBounds;// 获取可视区域的Bounds
     *          boolean isContain = latLngBounds.contains(marker.getPosition());// 判断上海经纬度是否包括在当前地图可见区域
     */


    public void toVisible(AMap aMap){
        LogUtil.d("5秒toVisible");
        if(listHaveBean){
                initBaseMarkerList(aMap);
                if(!markersIsVisible){
                    LatLngBounds latLngBounds = aMap.getProjection().getVisibleRegion().latLngBounds;
                    float zoomlevel = aMap.getCameraPosition().zoom;//缩放等级，越大显示范围越小。
                    for (Marker marker:baseMarkerList){

                        if(marker.getTitle().equals(GreenDaoUtils.TYPE_CAR)){
                            marker.setVisible(true);
                        }else if(marker.getTitle().equals(GreenDaoUtils.TYPE_SQUADRON)){
                            marker.setVisible(true);
                        }else if(marker.getTitle().equals(GreenDaoUtils.TYPE_RECAR)){
                            marker.setVisible(true);
                        } else if( zoomlevel >= Constants.MAX_ZOOM_VISIABLE_LEVEL){
                            if(latLngBounds.contains(marker.getPosition())){
                                marker.setVisible(true);
                            }else {
                                marker.setVisible(false);
                            }
                        }else {
                            marker.setVisible(false);
                        }
                    }
                    markersIsVisible = true;
                }
        }
    }

    public void toRefresh(Context mContext){
        LogUtil.d("5秒toRefresh");
        if(listHaveBean){
            if(isClicked){
                for(WarnMapData warnMapData : baseWarnMapDataList){
                    for (Marker marker:baseMarkerList){
                        if(marker.getSnippet().equals(warnMapData.getId())){
//                            marker.setPosition(new LatLng(24.573357, 118.092658));
                            LogUtil.d("5秒toRefresh ==mapType=="+mapType);
                            if(mapType == GreenDaoUtils.TYPE_RECAR){

                                View view = View.inflate(mContext, R.layout.view_infowindow_hydrant, null);
                                TextView title = view.findViewById(R.id.tv_title);
                                title.setText(warnMapData.getDeptName() + "   " + warnMapData.getFullName() == null ? warnMapData.getName() : warnMapData.getFullName());
                                TextView tvOne = view.findViewById(R.id.tv_one);
                                tvOne.setText(warnMapData.getPlateNo() +  (TextUtils.isEmpty(warnMapData.getSpeed())?"":("  "+warnMapData.getSpeed()+"km/h")));
                                BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(view);

                                MarkerOptions options =  marker.getOptions();
                                options.icon(bitmapDescriptor);
                                marker.setMarkerOptions(options);

                            }

                            marker.setPosition(warnMapData.getLatlng());

                        }
                    }
                }
            }
        }
    }

    public void toGone(){
        if(markersIsVisible){
            for (Marker marker:baseMarkerList){
                marker.setVisible(false);
            }
            markersIsVisible = false;
        }
    }

    public void showInfoWindow(){
        if(markersIsVisible){
            for (Marker marker:baseMarkerList){
                marker.showInfoWindow();
            }
        }
    }

    public void hideInfoWindow(){
        if(markersIsVisible){
            for (Marker marker:baseMarkerList){
                marker.hideInfoWindow();
            }
        }
    }

    public void toMove(AMap aMap){
        if(markersIsVisible){
            LatLngBounds latLngBounds = aMap.getProjection().getVisibleRegion().latLngBounds;
            float zoomlevel = aMap.getCameraPosition().zoom;//缩放等级，越大显示范围越小。
            for (Marker marker:baseMarkerList){
                if( zoomlevel >= Constants.MAX_ZOOM_VISIABLE_LEVEL){
                    if(latLngBounds.contains(marker.getPosition())){
                        marker.setVisible(true);
                    }else {
                        marker.setVisible(false);
                    }
                }else if(marker.getTitle().equals(GreenDaoUtils.TYPE_CAR)){
                    marker.setVisible(true);
                }else if(marker.getTitle().equals(GreenDaoUtils.TYPE_RECAR)){
                    marker.setVisible(true);
                }else if(marker.getTitle().equals(GreenDaoUtils.TYPE_PERSON)){
                    marker.setVisible(true);
                }else {
                    marker.setVisible(false);
                }
            }
        }
    }

    public void allClear(){
         listHaveBean = false;
         isLoadingData = false;
         aMapHaveMarkers = false;
         markersIsVisible = false;
        baseWarnMapDataList.clear();
        for(Marker marker : baseMarkerList){
            marker.remove();
        }
        baseMarkerList.clear();
         baseMarkerOptionsList.clear();
    }


    public List<Marker> getBaseMarkerList() {
        if(baseMarkerList == null) baseMarkerList = new ArrayList<>();
        return baseMarkerList;
    }

    public void setBaseMarkerList(List<Marker> baseMarkerList) {
        this.baseMarkerList = baseMarkerList;
    }

    public List<MarkerOptions> getBaseMarkerOptionsList() {
        return baseMarkerOptionsList;
    }

    public void setBaseMarkerOptionsList(List<MarkerOptions> baseMarkerOptionsList) {
        this.baseMarkerOptionsList = baseMarkerOptionsList;
    }

    public void addBaseMarkerOptionsList(MarkerOptions markerOptions) {
        baseMarkerOptionsList.add(markerOptions);
    }

    public synchronized void addBaseWarnMapDataList(WarnMapData warnMapData) {
        baseWarnMapDataList.add(warnMapData);
    }

    public List<WarnMapData> getBaseWarnMapDataList() {
        return baseWarnMapDataList;
    }

    public void setBaseWarnMapDataList(List<WarnMapData> baseWarnMapDataList) {
        this.baseWarnMapDataList = baseWarnMapDataList;
    }


    public boolean getListHaveBean() {
        return listHaveBean;
    }

    public void setListHaveBean(boolean listHaveBean) {
        this.listHaveBean = listHaveBean;
    }

    public boolean isLoadingData() {
        return isLoadingData;
    }

    public void setLoadingData(boolean loadingData) {
        isLoadingData = loadingData;
    }

    public boolean getaMapHaveMarkers() {
        return aMapHaveMarkers;
    }

    public void setaMapHaveMarkers(boolean aMapHaveMarkers) {
        this.aMapHaveMarkers = aMapHaveMarkers;
    }

    public boolean getMarkersIsVisible() {
        return markersIsVisible;
    }

    public void setMarkersIsVisible(boolean markersIsVisible) {
        this.markersIsVisible = markersIsVisible;
    }

    public String getMapType() {
        return mapType;
    }

    public void setMapType(String mapType) {
        this.mapType = mapType;
    }

    public boolean isClicked() {
        return isClicked;
    }

    public void setClicked(boolean clicked) {
        isClicked = clicked;
    }
}
